package com.secxiot.consumer.narcotic.controller;


import com.secxiot.common.constant.ServerStatusEnum;
import com.secxiot.common.entity.ResultVo;
import com.secxiot.common.utils.CachedBeanCopier;
import com.secxiot.consumer.narcotic.feign.NarcoticClient;
import com.secxiot.provider.narcotic.dto.NarcoticPatientDrugsRecordDto;
import com.secxiot.provider.narcotic.dto.NarcoticPatientInfoDto;
import com.secxiot.provider.narcotic.entity.NarcoticDocDo;
import com.secxiot.provider.narcotic.entity.TakeDestroyDo;
import com.secxiot.provider.narcotic.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Api(tags = {"麻醉药品跟踪"})
@RestController
@RequestMapping("/narcotics")
public class NarcoticConsumerController {

    @Autowired
    private NarcoticClient narcoticClient;
    private static final Logger LOG = LoggerFactory.getLogger(NarcoticConsumerController.class);

    @ApiOperation(value = "药品须知", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hospitalId", value = "医疗机构ID", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "type", value = "类型，数据字典", required = true, paramType = "query", dataType = "String")
    })
    @GetMapping("/narcoticDoc")
    public ResultVo<NarcoticDocVo> getNarcoticDoc(
            @RequestParam(value = "hospitalId", required = true) Long hospitalId,
            @RequestParam(value = "type", required = true) String type) {
        NarcoticDocVo narcoticDocVo = new NarcoticDocVo();
        if (hospitalId == null || hospitalId <= 0) {
            hospitalId = 21L;
        }
        NarcoticDocDo narcoticDocDo = narcoticClient.getNarcoticDoc(hospitalId, type);
        if (null != narcoticDocDo) {
            CachedBeanCopier.copy(narcoticDocDo, narcoticDocVo);
        }

        return new ResultVo<NarcoticDocVo>(narcoticDocVo);
    }

    @ApiOperation(value = "药品类型", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hospitalId", value = "医疗机构ID", required = true, paramType = "query", dataType = "Long")
    })
    @GetMapping("/narcoticDrugs/{hospitalId}")
    public ResultVo<List<NarcoticDrugsVo>> getNarcoticDrugs(
            @RequestParam(value = "hospitalId", required = true) Long hospitalId) {
        List<NarcoticDrugsVo> narcoticDrugsVoList = narcoticClient.selectNarcoticDrugs(hospitalId);
        return new ResultVo<List<NarcoticDrugsVo>>(narcoticDrugsVoList);
    }

    @ApiOperation(value = "用户资料信息--分页列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "条数", required = true, paramType = "query", dataType = "Integer")
    })
    @GetMapping("/narcoticUserInfo/pageList")
    public ResultVo<List<NarcoticPatientInfoVo>> selectNarcoticUserInfoPage(
            @RequestParam(value = "userId", required = true) Long userId,
            @RequestParam(value = "pageNo", required = true) Integer pageNo,
            @RequestParam(value = "pageSize", required = true) Integer pageSize) {
        return new ResultVo<List<NarcoticPatientInfoVo>>(narcoticClient.selectNarcoticPatientInfoPage(userId, pageNo, pageSize));
    }

    @ApiOperation(value = "用户资料信息--列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "Long")
    })
    @GetMapping("/narcoticUserInfo/list")
    public ResultVo<List<NarcoticPatientInfoVo>> selectNarcoticUserInfoList(
            @RequestParam(value = "userId", required = true) Long userId) {
        return new ResultVo<List<NarcoticPatientInfoVo>>(narcoticClient.selectNarcoticPatientInfoList(userId));
    }

    @ApiOperation(value = "用户资料信息--详情", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "patientInfoId", value = "用户资料id", required = true, paramType = "query", dataType = "Long")
    })
    @GetMapping("/narcoticUserInfo/detail")
    public ResultVo<NarcoticPatientInfoVo> getNarcoticUserInfoById(
            @RequestParam(value = "patientInfoId", required = true) Long patientInfoId) {
        return new ResultVo<NarcoticPatientInfoVo>(narcoticClient.getNarcoticPatientInfoById(patientInfoId));
    }

    @ApiOperation(value = "用户资料信息--添加", httpMethod = "POST")
    @PostMapping("/narcoticUserInfo/insert")
    public ResultVo<Long> insertNarcoticPatientInfo(@RequestBody NarcoticPatientInfoDto narcoticPatientInfoDto) {
        NarcoticPatientInfoVo narcoticPatientInfoVo = new NarcoticPatientInfoVo();
        CachedBeanCopier.copy(narcoticPatientInfoDto, narcoticPatientInfoVo);
        return new ResultVo<Long>(narcoticClient.insertNarcoticPatientInfo(narcoticPatientInfoVo));
    }

    @ApiOperation(value = "用户资料信息--修改", httpMethod = "POST")
    @PostMapping("/narcoticUserInfo/update")
    public ResultVo<Long> updateNarcoticPatientInfo(@RequestBody NarcoticPatientInfoDto narcoticUserInfoDto) {
        NarcoticPatientInfoVo narcoticUserInfoVo = new NarcoticPatientInfoVo();
        CachedBeanCopier.copy(narcoticUserInfoDto, narcoticUserInfoVo);
        return new ResultVo<Long>(narcoticClient.updateNarcoticPatientInfo(narcoticUserInfoVo));
    }

    @ApiOperation(value = "申请药品记录--列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "条数", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "isPage", value = "是否分页  0 否  1 是", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "searchName", value = "搜索关键字", required = false, paramType = "query", dataType = "String")
    })
    @GetMapping("/narcoticDrugsRecord/list")
    public ResultVo<List<NarcoticPatientDrugsRecordVo>> selectNarcoticDrugsRecord(
            @RequestParam(value = "userId", required = true) Long userId,
            @RequestParam(value = "pageNo", required = false) Integer pageNo,
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
            @RequestParam(value = "isPage", required = true) Integer isPage,
            @RequestParam(value = "searchName", required = false) String searchName) {
        if (isPage == 1) {
            if (pageNo == null) {
                return new ResultVo<List<NarcoticPatientDrugsRecordVo>>(ServerStatusEnum.PAGENO_EMPTY);
            }
            if (pageSize == null) {
                return new ResultVo<List<NarcoticPatientDrugsRecordVo>>(ServerStatusEnum.PAGESIZE_EMPTY);
            }
        }
        return new ResultVo<List<NarcoticPatientDrugsRecordVo>>(narcoticClient.selectNarcoticPatientDrugsRecord(userId, pageNo, pageSize, isPage, searchName));
    }

    @ApiOperation(value = "申请药品记录--添加", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hospitalId", value = "医疗而机构Id", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "drugsId", value = "药品类型Id", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "patientInfoId", value = "患者资料Id", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "userId", value = "用户Id", required = true, paramType = "query", dataType = "Long")
    })
    @GetMapping("/narcoticDrugsRecord/insert")
    public ResultVo<Long> insertNarcoticUserInfo(
            @RequestParam(value = "hospitalId", required = true) Long hospitalId,
            @RequestParam(value = "drugsId", required = true) Long drugsId,
            @RequestParam(value = "patientInfoId", required = true) Long patientInfoId,
            @RequestParam(value = "userId", required = true) Long userId) {
        NarcoticPatientDrugsRecordVo patientDrugsRecordVo = new NarcoticPatientDrugsRecordVo();
        patientDrugsRecordVo.setHospitalId(hospitalId);
        patientDrugsRecordVo.setDrugsId(drugsId);
        patientDrugsRecordVo.setPatientInfoId(patientInfoId);
        patientDrugsRecordVo.setUserId(userId);
        return new ResultVo<Long>(narcoticClient.insertNarcoticPatientDrugsRecord(patientDrugsRecordVo));
    }

    @ApiOperation(value = "申请药品记录--修改", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "patientDrugsRecordId", value = "申请药品记录ID", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "hospitalId", value = "医疗而机构Id", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "drugsId", value = "药品Id", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "patientInfoId", value = "患者资料Id", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "userId", value = "用户Id", required = true, paramType = "query", dataType = "Long")
    })
    @GetMapping("/narcoticDrugsRecord/update")
    public ResultVo<Long> updateNarcoticUserInfo(
            @RequestParam(value = "patientDrugsRecordId", required = true) Long patientDrugsRecordId,
            @RequestParam(value = "hospitalId", required = true) Long hospitalId,
            @RequestParam(value = "drugsId", required = true) Long drugsId,
            @RequestParam(value = "patientInfoId", required = true) Long patientInfoId,
            @RequestParam(value = "userId", required = true) Long userId) {
        NarcoticPatientDrugsRecordVo patientDrugsRecordVo = new NarcoticPatientDrugsRecordVo();
        patientDrugsRecordVo.setPatientDrugsRecordId(patientDrugsRecordId);
        patientDrugsRecordVo.setHospitalId(hospitalId);
        patientDrugsRecordVo.setDrugsId(drugsId);
        patientDrugsRecordVo.setPatientInfoId(patientInfoId);
        patientDrugsRecordVo.setUserId(userId);

        return new ResultVo<Long>(narcoticClient.updateNarcoticPatientDrugsRecord(patientDrugsRecordVo));
    }

    @ApiOperation(value = "医生药品记录--详情", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorRecordId", value = "医生药品记录ID", required = true, paramType = "query", dataType = "Long")
    })
    @GetMapping("/narcoticDoctorRecord/detail")
    NarcoticDoctorRecordVo getNarcoticDoctorRecordById(@RequestParam(value = "doctorRecordId", required = true) Long doctorRecordId) {
        return narcoticClient.getNarcoticDoctorRecordById(doctorRecordId);
    }

    /**
     * 医生药品记录 列表
     *
     * @param doctorUserId 医生用户ID
     * @param hospitalId   医疗机构ID
     * @return
     */
    @ApiOperation(value = "医生药品记录--列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorUserId", value = "医生用户ID", required = true, paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "hospitalId", value = "医疗机构Id", required = true, paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "条数", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "isPage", value = "是否分页  0 否  1 是", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "searchName", value = "搜索关键字", required = false, paramType = "query", dataType = "String")
    })
    @GetMapping("/narcoticDoctorRecord/list")
    ResultVo<List<NarcoticDoctorRecordVo>> selectNarcoticDoctorRecordList(
            @RequestParam(value = "doctorUserId", required = true) Long doctorUserId,
            @RequestParam(value = "hospitalId", required = true) Long hospitalId,
            @RequestParam(value = "pageNo", required = false) Integer pageNo,
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
            @RequestParam(value = "isPage", required = true) Integer isPage,
            @RequestParam(value = "searchName", required = false) String searchName) {
        if (isPage == 1) {
            if (pageNo == null) {
                return new ResultVo<List<NarcoticDoctorRecordVo>>(ServerStatusEnum.PAGENO_EMPTY);
            }
            if (pageSize == null) {
                return new ResultVo<List<NarcoticDoctorRecordVo>>(ServerStatusEnum.PAGESIZE_EMPTY);
            }
        }
        List<NarcoticDoctorRecordVo> narcoticDoctorRecordVos = narcoticClient.selectNarcoticDoctorRecordList(doctorUserId, hospitalId, pageNo, pageSize, isPage, searchName);
        return new ResultVo<List<NarcoticDoctorRecordVo>>(narcoticDoctorRecordVos);
    }

    /**
     * 医生药品记录 审核
     *
     * @return
     */
    @ApiOperation(value = "医生药品记录--审核", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorRecordId", value = "医生记录ID", required = true, paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "doctorUserId", value = "医生用户Id", required = true, paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "isAudit", value = "审核状态 1 审核通过  2 驳回", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "drugsId", value = "药品ID", required = false, paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "clinicalDiagnosis", value = "临床诊断", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "doseCount", value = "处方总量", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "doseOnce", value = "一次用量", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "hoursOnce", value = "间隔时间(时间/次)", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "remark", value = "备注", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "refuseReason", value = "驳回原因", required = false, paramType = "query", dataType = "String")
    })
    @GetMapping("/narcoticDoctorRecord/audit")
    ResultVo<NarcoticDoctorRecordVo> auditNarcoticDoctorRecord(
            @RequestParam(value = "doctorRecordId", required = true) Long doctorRecordId,
            @RequestParam(value = "doctorUserId", required = true) Long doctorUserId,
            @RequestParam(value = "isAudit", required = true) Integer isAudit,
            @RequestParam(value = "drugsId", required = false) Long drugsId,
            @RequestParam(value = "clinicalDiagnosis", required = false) String clinicalDiagnosis,
            @RequestParam(value = "doseCount", required = false) String doseCount,
            @RequestParam(value = "doseOnce", required = false) String doseOnce,
            @RequestParam(value = "hoursOnce", required = false) Integer hoursOnce,
            @RequestParam(value = "remark", required = false) String remark,
            @RequestParam(value = "refuseReason", required = false) String refuseReason) {
        NarcoticDoctorRecordVo narcoticDoctorRecordVo = new NarcoticDoctorRecordVo();
        narcoticDoctorRecordVo.setDoctorRecordId(doctorRecordId);
        narcoticDoctorRecordVo.setDoctorUserId(doctorUserId);
        narcoticDoctorRecordVo.setIsAudit(isAudit);
        narcoticDoctorRecordVo.setDrugsId(drugsId);

        if (isAudit == 1 || isAudit == 3) {
            String doseCountStr = doseCount.substring(doseCount.indexOf('*') + 1);
            String doseOnceStr = doseOnce.substring(doseOnce.indexOf('*') + 1);
            narcoticDoctorRecordVo.setDoseCount(getNum(doseCountStr));
            narcoticDoctorRecordVo.setDoseOnce(getNum(doseOnceStr));
            narcoticDoctorRecordVo.setHoursOnce(hoursOnce);
            narcoticDoctorRecordVo.setClinicalDiagnosis(clinicalDiagnosis);
        }

        narcoticDoctorRecordVo.setRemark(remark);
        narcoticDoctorRecordVo.setRefuseReason(refuseReason);
        return narcoticClient.auditNarcoticDoctorRecord(narcoticDoctorRecordVo);
    }

    /**
     * 获取用药下拉
     *
     * @param drugsId 药品的ID
     * @return 处方总量和每次用药量
     */
    @ApiOperation(value = "获取用药量下拉", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "drugsId", value = "药品Id", required = true, paramType = "query", dataType = "long")
    })
    @GetMapping("/getNarcoticDrugsById")
    ResultVo<NarcoticDrugs> getNarcoticDrugsById(@RequestParam("drugsId") Long drugsId) {
        NarcoticDrugsVo narcoticDrugsVo = narcoticClient.getNarcoticDrugsById(drugsId);

        List<String> doseCountList = new ArrayList<String>();
        List<String> doseOnceList = new ArrayList<String>();

        for (int i = 1; i < 501; i++) {
            //每次用量
            doseOnceList.add(narcoticDrugsVo.getSpec() + narcoticDrugsVo.getSpecName() + "*" + i + narcoticDrugsVo.getUnitName() + "/次");
            if (narcoticDrugsVo.getMeans().equals("0001900001")) {
                //处方总量
                doseCountList.add(narcoticDrugsVo.getSpec() + narcoticDrugsVo.getSpecName() + "*" + narcoticDrugsVo.getUnit() * i + narcoticDrugsVo.getUnitName());
            } else {
                //处方总量
                doseCountList.add(narcoticDrugsVo.getSpec() + narcoticDrugsVo.getSpecName() + "*" + i + narcoticDrugsVo.getUnitName());
            }
        }

        NarcoticDrugs narcoticDrugs = new NarcoticDrugs();
        narcoticDrugs.setDoseCountList(doseCountList);
        narcoticDrugs.setDoseOnceList(doseOnceList);
        return new ResultVo<NarcoticDrugs>(narcoticDrugs);
    }

    /****************************************药师药品记录**************************************************/
    @ApiOperation(value = "药师药品记录--详情", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "chemistRecordId", value = "药师记录id", required = true, paramType = "query", dataType = "long")
    })
    @GetMapping("/narcoticChemistRecord/detail")
    ResultVo<NarcoticChemistRecordVo> getNarcoticChemistRecordById(@RequestParam(value = "chemistRecordId", required = true) Long chemistRecordId) {
        return new ResultVo<NarcoticChemistRecordVo>(narcoticClient.getNarcoticChemistRecordById(chemistRecordId));
    }

    /**
     * 药师药品记录 列表
     *
     * @param chemistUserId 药师用户ID
     * @param hospitalId    医疗机构ID
     * @param pageNo        页数
     * @param pageSize      条数
     * @param isPage        是否分页  0 否  1 是
     * @param searchName    关键字
     * @return
     */
    @ApiOperation(value = "药师药品记录--列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "chemistUserId", value = "医生用户ID", required = true, paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "hospitalId", value = "医疗机构Id", required = true, paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "条数", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "isPage", value = "是否分页  0 否  1 是", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "searchName", value = "搜索关键字", required = false, paramType = "query", dataType = "String")
    })
    @GetMapping("/narcoticChemistRecord/list")
    ResultVo<List<NarcoticChemistRecordVo>> selectNarcoticChemistRecordList(
            @RequestParam(value = "chemistUserId", required = true) Long chemistUserId,
            @RequestParam(value = "hospitalId", required = true) Long hospitalId,
            @RequestParam(value = "pageNo", required = false) Integer pageNo,
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
            @RequestParam(value = "isPage", required = true) Integer isPage,
            @RequestParam(value = "searchName", required = false) String searchName) {
        if (isPage == 1) {
            if (pageNo == null) {
                return new ResultVo<List<NarcoticChemistRecordVo>>(ServerStatusEnum.PAGENO_EMPTY);
            }
            if (pageSize == null) {
                return new ResultVo<List<NarcoticChemistRecordVo>>(ServerStatusEnum.PAGESIZE_EMPTY);
            }
        }
        List<NarcoticChemistRecordVo> narcoticChemistRecordVos = narcoticClient.selectNarcoticChemistRecordList(chemistUserId, hospitalId, pageNo, pageSize, isPage, searchName);
        return new ResultVo<List<NarcoticChemistRecordVo>>(narcoticChemistRecordVos);
    }

    /**
     * 药师药品记录 审核
     *
     * @return
     */
    @ApiOperation(value = "药师药品记录--审核", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "chemistRecordId", value = "药师记录id", required = true, paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "chemistUserId", value = "药师用户Id", required = true, paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "isAudit", value = "审核状态 1 审核通过  2 驳回", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "remark", value = "备注", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "takeMedicineAddress", value = "取药地址", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "takeMedicineSetTime", value = "取药时间", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "refuseReason", value = "驳回原因", required = false, paramType = "query", dataType = "String")
    })
    @GetMapping("/narcoticChemistRecord/audit")
    ResultVo<NarcoticChemistRecordVo> auditNarcoticChemistRecord(
            @RequestParam(value = "chemistRecordId", required = true) Long chemistRecordId,
            @RequestParam(value = "chemistUserId", required = true) Long chemistUserId,
            @RequestParam(value = "isAudit", required = true) Integer isAudit,
            @RequestParam(value = "remark", required = false) String remark,
            @RequestParam(value = "takeMedicineAddress", required = false) String takeMedicineAddress,
            @RequestParam(value = "takeMedicineSetTime", required = false) String takeMedicineSetTime,
            @RequestParam(value = "refuseReason", required = false) String refuseReason
    ) {
        NarcoticChemistRecordVo narcoticChemistRecordVo = new NarcoticChemistRecordVo();
        narcoticChemistRecordVo.setChemistUserId(chemistUserId);
        narcoticChemistRecordVo.setChemistRecordId(chemistRecordId);
        narcoticChemistRecordVo.setIsAudit(isAudit);
        narcoticChemistRecordVo.setRemark(remark);
        narcoticChemistRecordVo.setTakeMedicineAddress(takeMedicineAddress);
        if (StringUtils.isNotBlank(takeMedicineSetTime)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                narcoticChemistRecordVo.setTakeMedicineSetTime(sdf.parse(takeMedicineSetTime));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        narcoticChemistRecordVo.setRefuseReason(refuseReason);
        return narcoticClient.auditNarcoticChemistRecord(narcoticChemistRecordVo);
    }

    /**
     * 取药销毁记录 信息
     *
     * @param takeDestroyId 取药记录id
     * @return
     */
    @ApiOperation(value = "取药记录--详情", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "takeDestroyId", value = "取药记录id", required = true, paramType = "query", dataType = "long"),
    })
    @GetMapping("/narcoticTakeDestroy/detail")
    ResultVo<NarcoticTakeDestroyVo> getNarcoticTakeDestroyById(@RequestParam("takeDestroyId") Long takeDestroyId) {
        return new ResultVo<NarcoticTakeDestroyVo>(narcoticClient.getNarcoticTakeDestroyById(takeDestroyId));
    }

    /**
     * 取药销毁记录 列表
     *
     * @param hospitalId 医疗机构ID
     * @param type       1 取药 2 销毁
     * @param pageNo     页数
     * @param pageSize   条数
     * @param isPage     是否分页  0 否  1 是
     * @param searchName 关键字
     * @return
     */
    @ApiOperation(value = "取药销毁记录--列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hospitalId", value = "医疗机构Id", required = true, paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "type", value = "1 取药  2 销毁", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "isPage", value = "是否分页  0 否  1 是", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "条数", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "searchName", value = "搜索关键字", required = false, paramType = "query", dataType = "String")
    })
    @GetMapping("/narcoticTakeDestroy/list")
    ResultVo<List<NarcoticTakeDestroyVo>> selectNarcoticTakeDestroy(
            @RequestParam("hospitalId") Long hospitalId,
            @RequestParam("type") String type,
            @RequestParam("isPage") Integer isPage,
            @RequestParam("pageNo") Integer pageNo,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam("searchName") String searchName) {
        if (isPage == 1) {
            if (pageNo == null) {
                return new ResultVo<List<NarcoticTakeDestroyVo>>(ServerStatusEnum.PAGENO_EMPTY);
            }
            if (pageSize == null) {
                return new ResultVo<List<NarcoticTakeDestroyVo>>(ServerStatusEnum.PAGESIZE_EMPTY);
            }
        }
        List<NarcoticTakeDestroyVo> narcoticTakeDestroyVos = narcoticClient.selectNarcoticTakeDestroy(hospitalId, type, pageNo, pageSize, isPage, searchName);
        return new ResultVo<List<NarcoticTakeDestroyVo>>(narcoticTakeDestroyVos);
    }

    /**
     * 患者取药
     *
     * @param takeDestroyDo 取药入参
     * @return
     */
    @ApiOperation(value = "取药", httpMethod = "POST")
    @PostMapping("/narcoticTakeDestroy/takeMedicine")
    ResultVo<NarcoticTakeDestroyVo> auditNarcoticTakeMedicine(@RequestBody TakeDestroyDo takeDestroyDo) {
        return narcoticClient.auditNarcoticTakeMedicine(takeDestroyDo);
    }

    /**
     * 销毁药瓶
     *
     * @param id
     * @param destroyFile   销毁附件
     * @param destroyUserId 销毁人
     * @return
     */
    /*@ApiOperation(value = "销毁", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "取药销毁id", required = true, paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "destroyFile", value = "销毁附件", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "destroyUserId", value = "销毁人Id", required = true, paramType = "query", dataType = "long")
    })
    @GetMapping("/narcoticTakeDestroy/destoryMedicine")
    ResultVo<NarcoticTakeDestroyVo> auditNarcoticDestroy(
            @RequestParam("id") Long id,
            @RequestParam("destroyFile") String destroyFile,
            @RequestParam("destroyUserId") Long destroyUserId) {
        return narcoticClient.auditNarcoticDestroy(id, destroyFile, destroyUserId);
    }*/

    /**
     * 监管审核资料审核
     *
     * @return
     */
    @ApiOperation(value = "监管资料--审核", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "patientInfoId", value = "用户资料id", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "isAudit", value = "审核状态 1 审核通过  2 驳回", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "refuseReason", value = "驳回原因", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "diagnose", value = "诊断内容", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "auditUserId", value = "监管用户id", required = false, paramType = "query", dataType = "Long")
    })
    @GetMapping("/narcoticUserInfo/superviseAudit")
    ResultVo<NarcoticPatientInfoVo> auditNarcoticPatientInfo(
            @RequestParam(value = "patientInfoId", required = true) Long patientInfoId,
            @RequestParam(value = "isAudit", required = true) Integer isAudit,
            @RequestParam(value = "refuseReason", required = false) String refuseReason,
            @RequestParam(value = "diagnose", required = false) String diagnose,
            @RequestParam(value = "auditUserId", required = true) Long auditUserId) {
        NarcoticPatientInfoVo narcoticPatientInfoVo = new NarcoticPatientInfoVo();
        narcoticPatientInfoVo.setPatientInfoId(patientInfoId);
        narcoticPatientInfoVo.setIsAudit(isAudit);
        narcoticPatientInfoVo.setRefuseReason(refuseReason);
        narcoticPatientInfoVo.setDiagnose(diagnose);
        narcoticPatientInfoVo.setAuditUserId(auditUserId);
        return narcoticClient.auditNarcoticPatientInfo(narcoticPatientInfoVo);
    }

    /**
     * 监管资料列表
     *
     * @param hospitalId 医疗机构ID
     * @param pageNo     页数
     * @param pageSize   条数
     * @param isPage     是否分页  0 否  1 是
     * @return 用户资料信息
     */
    @ApiOperation(value = "监管资料--列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hospitalId", value = "医疗机构Id", required = true, paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "isPage", value = "是否分页  0 否  1 是", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "条数", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "searchName", value = "搜索关键字", required = false, paramType = "query", dataType = "String")
    })
    @GetMapping("/narcoticUserInfo/superviseList")
    ResultVo<List<NarcoticPatientInfoVo>> selectNarcoticPatientInfoPageByHospital(
            @RequestParam(value = "hospitalId") Long hospitalId,
            @RequestParam(value = "pageNo") Integer pageNo,
            @RequestParam(value = "pageSize") Integer pageSize,
            @RequestParam(value = "isPage") Integer isPage,
            @RequestParam(value = "searchName") String searchName) {
        if (isPage == 1) {
            if (pageNo == null) {
                return new ResultVo<List<NarcoticPatientInfoVo>>(ServerStatusEnum.PAGENO_EMPTY);
            }
            if (pageSize == null) {
                return new ResultVo<List<NarcoticPatientInfoVo>>(ServerStatusEnum.PAGESIZE_EMPTY);
            }
        }
        List<NarcoticPatientInfoVo> narcoticUserInfoVos = narcoticClient.selectNarcoticPatientInfoPageByHospital(hospitalId, pageNo, pageSize, isPage, searchName);
        return new ResultVo<List<NarcoticPatientInfoVo>>(narcoticUserInfoVos);
    }

    /**
     * 药品跟踪记录--详情
     *
     * @param patientDrugsRecordId 申请用药记录ID
     * @return NarcoticDrugsRecordDo
     */
    @ApiOperation(value = "药品跟踪记录--详情", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "patientDrugsRecordId", value = "申请用药记录Id", required = true, paramType = "query", dataType = "long")
    })
    @GetMapping("/narcoticDrugsRecordDto/detail")
    ResultVo<NarcoticPatientDrugsRecordDto> getNarcoticPatientDrugsRecordDtoById(@RequestParam("patientDrugsRecordId") Long patientDrugsRecordId) {
        return narcoticClient.getNarcoticPatientDrugsRecordDtoById(patientDrugsRecordId);
    }

    /**
     * 药品厂家列表
     *
     * @param hospitalId 医院ID
     * @return
     */
    @ApiOperation(value = "药品厂家--列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hospitalId", value = "医院Id", required = true, paramType = "query", dataType = "long")
    })
    @GetMapping("/narcoticDrugsFactory/list")
    ResultVo<List<NarcoticDrugsFactoryVo>> getNarcoticDrugsFactoryList(@RequestParam("hospitalId") Long hospitalId) {
        List<NarcoticDrugsFactoryVo> narcoticDrugsFactoryList = narcoticClient.getNarcoticDrugsFactoryList(hospitalId);
        return new ResultVo<List<NarcoticDrugsFactoryVo>>(narcoticDrugsFactoryList);
    }

    /**
     * 扫码验证药品
     *
     * @param patientDrugsRecordId 申请用药记录Id
     * @param patientUserId        患者用户Id
     * @return
     */
    @ApiOperation(value = "扫码验证药品", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "patientDrugsRecordId", value = "申请用药记录Id", required = true, paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "patientUserId", value = "患者用户Id", required = true, paramType = "query", dataType = "long")
    })
    @GetMapping("/scanDrugsQRCode")
    QRCodeProDrugsVo scanDrugsQRCode(@RequestParam("patientDrugsRecordId") Long patientDrugsRecordId,
                                     @RequestParam("patientUserId") Long patientUserId) {
        return narcoticClient.scanDrugsQRCode(patientDrugsRecordId, patientUserId);
    }

    /**
     * 获取药品统计数据
     *
     * @param userId   用户ID
     * @param dateTime 时间节点
     * @return
     */
    @ApiOperation(value = "获取药品统计数据", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "dateTime", value = "时间节点", required = true, paramType = "query", dataType = "Date"),
    })
    @GetMapping("/getDrugsCountResultVo")
    DrugsCountResultVo getDrugsCountResultVo(@RequestParam("userId") Long userId, @RequestParam("dateTime") Date dateTime) {
        return narcoticClient.getDrugsCountResultVo(userId, dateTime);
    }

    /**
     * 获取用户有效资料
     *
     * @param userId 用户Id
     */
    @ApiOperation(value = "获取用户有效资料列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query", dataType = "long"),
    })
    @GetMapping("/getPatientInfoByUserId")
    public List<NarcoticPatientInfoVo> getPatientInfoByUserId(@RequestParam("userId") Long userId) {
        return narcoticClient.getPatientInfoByUserId(userId);
    }

    /**
     * 获取用户未完成的用药申请列表
     *
     * @param userId 用户Id
     */
    @ApiOperation(value = "获取用户未完成的用药申请列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query", dataType = "long"),
    })
    @GetMapping("/getPatientDrugsRecordByUserId")
    public List<NarcoticPatientDrugsRecordVo> getPatientDrugsRecordByUserId(@RequestParam("userId") Long userId) {
        return narcoticClient.getPatientDrugsRecordByUserId(userId);
    }

    /**
     * 药师药品记录 审核
     *
     * @return
     */
    @ApiOperation(value = "药师重置审核", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "chemistRecordId", value = "药师记录id", required = true, paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "chemistUserId", value = "药师用户Id", required = true, paramType = "query", dataType = "long")
    })
    @GetMapping("/narcoticChemistRecord/resetAudit")
    Long resetAuditNarcoticChemistRecord(
            @RequestParam(value = "chemistRecordId", required = true) Long chemistRecordId,
            @RequestParam(value = "chemistUserId", required = true) Long chemistUserId) {
        return narcoticClient.resetAuditNarcoticChemistRecord(chemistRecordId, chemistUserId);
    }

    /**
     * 截取字符串中的数字
     *
     * @param str
     * @return
     */
    private static Integer getNum(String str) {
        String regEx = "[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        String trim = m.replaceAll("").trim();
        if (trim.length() == 1) {
            trim = "0" + trim;
        }
        return Integer.valueOf(trim);
    }
}
